import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import '../../../core/providers/global_state_coordinator.dart';
import '../widgets/user_content_grid.dart';

/// 用户喜欢内容页面
class UserFavoritesTab extends StatefulWidget {
  const UserFavoritesTab({super.key});

  @override
  State<UserFavoritesTab> createState() => _UserFavoritesTabState();
}

class _UserFavoritesTabState extends State<UserFavoritesTab>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Consumer<GlobalStateCoordinator>(
      builder: (context, coordinator, child) {
        final isLoggedIn = coordinator.isLoggedIn;

        if (!isLoggedIn) {
          return _buildLoginPrompt();
        }

        return const UserContentGrid(contentType: UserContentType.favorites);
      },
    );
  }

  /// 构建登录提示
  Widget _buildLoginPrompt() {
    return SingleChildScrollView(
      child: Container(
        height: MediaQuery.of(context).size.height * 0.3,
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                Icons.favorite_border,
                size: 50.w,
                color: Colors.white.withValues(alpha: 0.5),
              ),

              SizedBox(height: 20.h),

              Text(
                'Login to view your favorites',
                style: TextStyle(
                  fontSize: 18.sp,
                  color: Colors.white.withValues(alpha: 0.7),
                  fontFamily: 'Poppins',
                ),
              ),

              SizedBox(height: 20.h),

              ElevatedButton(
                onPressed: _handleLoginTap,
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.amber[100],
                  foregroundColor: Colors.brown[700],
                  padding: EdgeInsets.symmetric(
                    horizontal: 32.w,
                    vertical: 12.h,
                  ),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(25.w),
                  ),
                ),
                child: Text(
                  'Login',
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                    fontFamily: 'Poppins',
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  /// 处理登录按钮点击
  void _handleLoginTap() {
    Navigator.pushNamed(context, '/login');
  }
}
